package com.zxd.socket.service;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;

/**
 * ClassName:Target
 * Package:com.zxd.socket.service
 *
 * @Date:2023/10/6 18:04
 * @Author:zxde
 */
public class Target implements Runnable {
    private Socket accept;

    public Target(Socket socket){
        this.accept = socket;
    }

    @Override
    public void run() {
        System.out.println(accept.getInetAddress()+"上线,端口："+accept.getPort());

        DataInputStream dis = null;

        try {
            dis = new DataInputStream(accept.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }


        while (true){

            try {
                String s = dis.readUTF();
                System.out.println(s);
            } catch (IOException e) {
                System.out.println(accept.getInetAddress()+"下线,端口："+accept.getPort());
                break;
            }
        }

        try {
            dis.close();
            accept.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
